package com.encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.taglibs.standard.lang.jpath.encoding.HtmlEncoder;

import au.com.bytecode.opencsv.CSVReader;

public class EncoderTest {

	public static void main(String args[]){
		//String str="<P ALIGN=\"LEFT\"></P><P ALIGN=\"LEFT\">&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;//www.youtube.com/embed/Zxnx3W-HA18&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;</P>";
		String str="(A312) 0xc2 0xbd mile";
		
		File folder = new File("C:/Users/aajjapla/Desktop/tru/UK/feed/store locator special char issue");
         
		File[] feedFiles = folder.listFiles();
        if(feedFiles !=null && feedFiles.length > 0){
        	 for(File feedFile:feedFiles){
        		 getCSVData(feedFile);
        	 }
        }
		
		try {
			
			System.out.println(URLDecoder.decode(str,"UTF-8"));
			System.out.println(URLDecoder.decode(str,"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(StringEscapeUtils.unescapeHtml4(str));
		
	}
	
	
	public static Map<String, List<String[]>> getCSVData(File dataFile) {

		Map<String, List<String[]>> dataMap = null;
		CSVReader csvReader = null;
		List<String[]> csvData = null;

		try {
			if (dataFile != null) {
			    InputStreamReader reader = new InputStreamReader(new FileInputStream(dataFile));
				csvReader = new CSVReader(reader, ',', '\0', '\0', 0, false, false);
				csvData = csvReader.readAll();

				if (csvData.size() > 1) {
					dataMap = new HashMap<String, List<String[]>>();
					List<String[]> header = new ArrayList<String[]>();

					String[] headerData = csvData.get(0);
					for (int index =0; index < headerData.length; index++){
					    headerData[index] = headerData[index].toLowerCase();
					    System.out.println("@@@@@@@ "+index +" : "+headerData[index]);
					}

					header.add(headerData);
                    csvData.remove(0);

					dataMap.put("header", header);
					dataMap.put("data", csvData);
				} 


			} 

		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}finally{
			if(csvReader!=null)
				try {
					csvReader.close();
				} catch (IOException e) {
					System.out.println(e.getMessage());
				}
		}

		return dataMap;
	}
}
